<?php

/**
 * This is the model class for table "{{faq}}".
 *
 * The followings are the available columns in table '{{faq}}':
 * @property integer $faq_id
 * @property integer $faq_author
 * @property string $faq_question
 * @property integer $faq_status
 * @property string $faq_answer
 */
class Faq extends CActiveRecord
{
	
	const FAQ_PRIVATE = 0;
	const FAQ_PUBLIC = 1;
	const FAQ_TRASH = 2;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Faq the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{faq}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('faq_question, faq_answer', 'required'),
			array('faq_author, faq_status', 'numerical', 'integerOnly'=>true),
			array('faq_question', 'length', 'max'=>500),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('faq_id, faq_author, faq_status, faq_question, faq_answer', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'faq_id' => Yii::t('faq','Faq Id'),
			'faq_author' => Yii::t('faq','Faq Author'),
			'faq_status' => Yii::t('faq','Faq Status'),
			'faq_question' => Yii::t('faq','Faq Question'),
			'faq_answer' => Yii::t('faq','Faq Answer'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('faq_id',$this->faq_id);
		$criteria->compare('faq_author',$this->faq_author);
		$criteria->compare('faq_question',$this->faq_question,true);
		$criteria->compare('faq_answer',$this->faq_answer,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getFaqStatus($status)
	{
		switch ($status)
		{
			case self::FAQ_PRIVATE:
				return Yii::t('common','Private');
				break;
			case self::FAQ_PUBLIC:
				return  Yii::t('common','Public');
				break;
			case self::FAQ_TRASH:
				return Yii::t('common','Trash');
				break;
		}
	}
	
	public function generateFaqStatusDropDownList()
	{
		return array(
			self::FAQ_PUBLIC => self::getFaqStatus(self::FAQ_PUBLIC),
			self::FAQ_PRIVATE => self::getFaqStatus(self::FAQ_PRIVATE),
			self::FAQ_TRASH => self::getFaqStatus(self::FAQ_TRASH)
		);
	}
	
	
	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->faq_author = Yii::app()->user->id;
				$this->faq_status = self::FAQ_PUBLIC;
			}
			return true;
		}
		else
			return false;
	}
}